<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - save_webp.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2022  Davis E. King (davis@dlib.net), Adrià Arrufat
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_SAVE_WEBP_Hh_
<font color='#0000FF'>#define</font> DLIB_SAVE_WEBP_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='save_webp_abstract.h.html'>save_webp_abstract.h</a>"

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../enable_if.h.html'>../enable_if.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='image_saver.h.html'>image_saver.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../array2d.h.html'>../array2d.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../pixel.h.html'>../pixel.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../image_processing/generic_image.h.html'>../image_processing/generic_image.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>string<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> impl
    <b>{</b>
        <font color='#0000FF'>enum</font> <font color='#0000FF'>class</font> <b><a name='webp_type'></a>webp_type</b>
        <b>{</b>
            rgb,
            bgr,
            rgba,
            bgra
        <b>}</b>;

        <font color='#0000FF'><u>void</u></font> <b><a name='impl_save_webp'></a>impl_save_webp</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> filename,
            <font color='#0000FF'>const</font> uint8_t<font color='#5555FF'>*</font> data,
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> width,
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> height,
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> stride,
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>float</u></font> quality,
            <font color='#0000FF'>const</font> webp_type type
        <font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> image_type
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>typename</font> disable_if<font color='#5555FF'>&lt;</font>is_matrix<font color='#5555FF'>&lt;</font>image_type<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font>::type <b><a name='save_webp'></a>save_webp</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> image_type<font color='#5555FF'>&amp;</font> img_,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> filename,
        <font color='#0000FF'><u>float</u></font> quality <font color='#5555FF'>=</font> <font color='#979000'>75</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
<font color='#0000FF'>#ifndef</font> DLIB_WEBP_SUPPORT
            <font color='#009900'>/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                You are getting this error because you are trying to use the save_webp
                function but you haven't defined DLIB_WEBP_SUPPORT.  You must do so to use
                this object.   You must also make sure you set your build environment
                to link against the libwebp library.
            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/</font>
            <font color='#BB00BB'>static_assert</font><font face='Lucida Console'>(</font><font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>image_type<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>, "<font color='#CC0000'>webp support not enabled.</font>"<font face='Lucida Console'>)</font>;
<font color='#0000FF'>#endif</font>
        const_image_view<font color='#5555FF'>&lt;</font>image_type<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>img</font><font face='Lucida Console'>(</font>img_<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>using</font> pixel_type <font color='#5555FF'>=</font> <font color='#0000FF'>typename</font> image_traits<font color='#5555FF'>&lt;</font>image_type<font color='#5555FF'>&gt;</font>::pixel_type;

        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>img.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
            "<font color='#CC0000'>\t save_webp()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You can't save an empty image as a WEBP.</font>"
            <font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> quality,
            "<font color='#CC0000'>\t save_webp()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid quality value.</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t quality: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> quality
            <font face='Lucida Console'>)</font>;

        <font color='#0000FF'>auto</font> data <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'>const</font> uint8_t<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>image_data</font><font face='Lucida Console'>(</font>img<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> width <font color='#5555FF'>=</font> img.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> height <font color='#5555FF'>=</font> img.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>int</u></font> stride <font color='#5555FF'>=</font> <font color='#BB00BB'>width_step</font><font face='Lucida Console'>(</font>img<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>pixel_traits<font color='#5555FF'>&lt;</font>pixel_type<font color='#5555FF'>&gt;</font>::rgb_alpha<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>pixel_traits<font color='#5555FF'>&lt;</font>pixel_type<font color='#5555FF'>&gt;</font>::bgr_layout<font face='Lucida Console'>)</font>
                impl::<font color='#BB00BB'>impl_save_webp</font><font face='Lucida Console'>(</font>filename, data, width, height, stride, quality, impl::webp_type::bgra<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>else</font>
                impl::<font color='#BB00BB'>impl_save_webp</font><font face='Lucida Console'>(</font>filename, data, width, height, stride, quality, impl::webp_type::rgba<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>pixel_traits<font color='#5555FF'>&lt;</font>pixel_type<font color='#5555FF'>&gt;</font>::rgb<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>pixel_traits<font color='#5555FF'>&lt;</font>pixel_type<font color='#5555FF'>&gt;</font>::bgr_layout<font face='Lucida Console'>)</font>
                impl::<font color='#BB00BB'>impl_save_webp</font><font face='Lucida Console'>(</font>filename, data, width, height, stride, quality, impl::webp_type::bgr<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>else</font>
                impl::<font color='#BB00BB'>impl_save_webp</font><font face='Lucida Console'>(</font>filename, data, width, height, stride, quality, impl::webp_type::rgb<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#009900'>// This is some other kind of color image so just save it as an RGB image.
</font>            array2d<font color='#5555FF'>&lt;</font>rgb_pixel<font color='#5555FF'>&gt;</font> temp;
            <font color='#BB00BB'>assign_image</font><font face='Lucida Console'>(</font>temp, img<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>auto</font> data <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'>const</font> uint8_t<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>image_data</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            impl::<font color='#BB00BB'>impl_save_webp</font><font face='Lucida Console'>(</font>filename, data, width, height, stride, quality, impl::webp_type::rgb<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> EXP
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='save_webp'></a>save_webp</b><font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> matrix_exp<font color='#5555FF'>&lt;</font>EXP<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> img,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> filename,
        <font color='#0000FF'><u>float</u></font> quality <font color='#5555FF'>=</font> <font color='#979000'>75</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        array2d<font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> EXP::type<font color='#5555FF'>&gt;</font> temp;
        <font color='#BB00BB'>assign_image</font><font face='Lucida Console'>(</font>temp, img<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>save_webp</font><font face='Lucida Console'>(</font>temp, filename, quality<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_WEBP_SUPPORT
</font>
</pre></body></html>